/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhelper.models;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.lang.model.element.Element;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Administrator
 */
public class AddressBook {
    
    public boolean newCard(AddressBookBean newcard) throws IOException, ParserConfigurationException, SAXException{
        
        Document doc;
        
        Element addressbook,card;
        
        Element name,mobile,office,home,qq,email,other;
        
        boolean flag = false;
        
        try{
            DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = f.newDocumentBuilder();
            File file = new File("addressbook");
            if(!file.exists()){
                file.mkdir();
            }
            file = new File("addressbook/addressbook.xml");
            if(!file.exists()){
                flag = true;
                file.createNewFile();
                doc = (Document) db.newDocument();
                addressbook = doc.createElement("addressbook");
            }else{
                doc = (Document) db.parse(file);
                addressbook = (Element) doc.getElementsByTagName("addressbook").item(0);
            }
            
            card = doc.createElement("card");
            name = doc.createElement("name");
            mobile = doc.createElement("mobile");
            office = doc.createElement("office");
            home = doc.createElement("home");
            qq = doc.createElement("qq");
            email = doc.createElement("email");
            other = doc.createElement("other");
            
            name.appendChild(doc.createTextNode(newcard.getName()));
            mobile.appendChild(doc.createTextNode(newcard.getMobile()));
            office.appendChild(doc.createTextNode(newcard.getOffice()));
            home.appendChild(doc.createTextNode(newcard.getHome()));
            qq.appendChild(doc.createTextNode(newcard.getQq()));
            email.appendChild(doc.createTextNode(newcard.getEmail()));
            other.appendChild(doc.createTextNode(newcard.getOther()));
            
            card.appendChild(name);
            card.appendChild(mobile);
            card.appendChild(office);
            card.appendChild(home);
            card.appendChild(qq);
            card.appendChild(email);
            card.appendChild(other);
            
            addressbook.appendChild(card);
            if(flag){
                doc.appendChild(addressbook);
            }
            TransformerFactory ff = TransformerFactory.newInstance();
        }
        
        
        
        
        return true;
        
    }
    
    public boolean modifyCard(AddressBookBean oldcard){
        
        return true;
    }
    
    
    public boolean delCard(String name){
        
        return true;
    }
    
    
    
    public Hashtable getAllCards(){
        
        return null;
    }
    
    public static void main(String[] args) {
        
        AddressBookBean newcard = new AddressBookBean("张思","13557575757","123123","123488""aa@","no");
        new AddressBook().newCard(newcard);
        
        
    }
    

}
